import { EntityManager, MikroORM } from '@mikro-orm/mysql';
import { InjectEntityManager, InjectMikroORM } from '@mikro-orm/nestjs';
import { Controller, Get, Inject } from '@nestjs/common';
import { ApiTags } from '@nestjs/swagger';
import { JsonApiResponse } from '@utils/json.api.response';
import { knext } from 'knext';
import { TypechoMetas } from '@databases/entities/typecho/TypechoMetas';
import { toTree } from '@zhengxs/js.tree';
import { ConfigService } from '@nestjs/config';
import { QiniuService } from '../service/qiniu.service';

@ApiTags('(站点) 设置')
@Controller('setting')
export class SettingController {
    constructor(
        private readonly configService: ConfigService,
        @InjectMikroORM('zp')
        private readonly orm: MikroORM,
        @InjectEntityManager('zp')
        private readonly em: EntityManager, 

        @InjectMikroORM('typecho')
        private readonly ormTypecho: MikroORM,
        @InjectEntityManager('typecho')
        private readonly emTypecho: EntityManager,
        
        private readonly qiniuService : QiniuService
    ) { }

    @Get('/')
    index() {
        return JsonApiResponse.new({
            serverUrl:'ws://192.168.1.6:3000/mqtt'
        })
    }

    @Get('/tree')
    async tree() {
        let knex =  knext(this,this.ormTypecho,this.emTypecho)

        let nodes = await knex.fromORM(TypechoMetas)
        
        let tree = toTree(nodes,{
            idKey:'mid',
            parentKey:'parent'
        })
        
        return JsonApiResponse.new(tree)
    }

    @Get('config')
    config(){
        const url = this.configService.get<string>('qiniu.url')

        this.qiniuService.test()
    
        return JsonApiResponse.string(url)
    }
}
